package com.ludomancy.breathe;

import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;

import org._3pq.jgrapht.Edge;
import org._3pq.jgrapht.UndirectedGraph;
import org._3pq.jgrapht.WeightedGraph;
import org._3pq.jgrapht.edge.EdgeFactories;
import org._3pq.jgrapht.edge.UndirectedWeightedEdge;
import org._3pq.jgrapht.graph.AbstractBaseGraph;

public class PsycheGraph extends AbstractBaseGraph implements UndirectedGraph, WeightedGraph
{
	public PsycheGraph() 
	{
	    super(new EdgeFactories.UndirectedWeightedEdgeFactory(), false, true);
	}
		
	public Edge addEdge(PsycheNode n1, PsycheNode n2, double weight)
	{
		UndirectedWeightedEdge newEdge = (UndirectedWeightedEdge) this.getEdgeFactory().createEdge(n1, n2);
		newEdge.setWeight(weight);
		addEdge(newEdge);
		return newEdge;
	}
	
	public double getWeight(PsycheNode n1, PsycheNode n2)
	{
		UndirectedWeightedEdge edge = (UndirectedWeightedEdge) getEdge(n1, n2);
		if(edge != null)
		{
			return edge.getWeight();
		}
		return 0;
	}
	
	public PsycheNode getNode(String id)
	{
		for (Iterator iter = vertexSet().iterator(); iter.hasNext();)
		{
			PsycheNode node = (PsycheNode) iter.next();
			if(node.id.equals(id))
			{
				return node;
			}
		}
		return null;
	}
	
	public Collection getNeighbors(PsycheNode node)
	{
		Vector ret = new Vector();
		Collection edges = this.edgesOf(node);
		for (Iterator iter = edges.iterator(); iter.hasNext();)
		{
			Edge element = (Edge) iter.next();
			ret.add((PsycheNode) element.oppositeVertex(node));
		}
		return ret;
	}
}
